package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.domain.Dish;

import com.itheima.domain.Setmeal;
import com.itheima.domain.SetmealDish;
import com.itheima.dto.DishDto;
import com.itheima.domain.DishFlavor;
import com.itheima.mapper.DishMapper;
import com.itheima.service.DishFlavorService;
import com.itheima.service.DishService;
import com.itheima.service.SetmealDishService;
import com.itheima.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private SetmealService setmealService;

    /**
     * 新增菜品，同时保存对应的口味数据
     * @param dishDto
     */
    @Override
    public void saveWithFlavor(DishDto dishDto) {
        //保存菜品的基本信息到菜品表dish
        this.save(dishDto);
        //获得菜品id
        Long dishDtoId = dishDto.getId();
        //菜品口味
        List<DishFlavor> flavors = dishDto.getFlavors();

        flavors=flavors.stream().map((item)->{
            item.setDishId(dishDtoId);
            return item;
        }).collect(Collectors.toList());

        //保存菜品口味数据到菜品口味表dish_flavor
        dishFlavorService.saveBatch(flavors);
    }

    @Override
    public DishDto getByIdWithFlavor(Long id) {
        Dish dish = this.getById(id);
        DishDto dishDto=new DishDto();
        BeanUtils.copyProperties(dish,dishDto);
        LambdaQueryWrapper<DishFlavor> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dish.getId());
        List<DishFlavor> flavors = dishFlavorService.list(queryWrapper);
        dishDto.setFlavors(flavors);
        return dishDto;
    }

    @Override
    public void updateWithFlavor(DishDto dishDto) {
        //更新dish表基本信息
        this.updateById(dishDto);

        //清理当前菜品对应口味数据---dish_flavor表的delete操作
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(DishFlavor::getDishId,dishDto.getId());

        dishFlavorService.remove(queryWrapper);

        //添加当前提交过来的口味数据---dish_flavor表的insert操作
        List<DishFlavor> flavors = dishDto.getFlavors();

        flavors = flavors.stream().map((item) -> {
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());

        dishFlavorService.saveBatch(flavors);


    }

    /**
     * 批量修改菜品启售和停售状态;
     * @param status
     * @param ids
     */
    @Override
    @Transactional
    public void updateStatus(Integer status, List<Long> ids) {
        //1.首先判断本次操作是停售还是启售,如果是启售,就直接执行修改的操作;
        if(status == 1){
            LambdaUpdateWrapper<Dish> updateWrapper = new LambdaUpdateWrapper();
            updateWrapper.in(Dish::getId,ids);
            updateWrapper.set(Dish::getStatus,status);
            this.update(updateWrapper);
            return;
        }
        //2.如果本次是停售,首先要查询需要修改的菜品是否在在售的套餐中出售,如果不在套餐中可以直接修改;
        for (Long id : ids) {
            LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SetmealDish::getDishId,id);
            //2.1一个菜品可能在多个套餐中存在
            List<SetmealDish> list = setmealDishService.list(queryWrapper);
            //集合为空,或者长度为0;则表示菜品不在任何套餐中,则直接修改该菜品的状态;
            if(list == null || list.size() == 0){
                LambdaUpdateWrapper<Dish> updateWrapper = new LambdaUpdateWrapper();
                updateWrapper.eq(Dish::getId,id);
                updateWrapper.set(Dish::getStatus,status);
                this.update(updateWrapper);
            }else{
                //3.如果菜品在套餐中存在,则需要判断套餐目前的状态;遍历集合,取出菜品对应的每一个套餐;
                for (SetmealDish setmealDish : list) {
                    Long setmealId = setmealDish.getSetmealId();
                    //3.1根据套餐id查询套餐的状态;
                    Setmeal setmeal = setmealService.getById(setmealId);

                    //3.2如果套餐状态是启售,则需要先把套餐的出售状态也要改为停售,再将菜品状态改为停售;
                    if(setmeal.getStatus() == 1){
                        setmeal.setStatus(status); //将该套餐的状态改为0,停售;
                        setmealService.updateById(setmeal);
                    }
                    //3.2如果套餐状态是停售,则直接将菜品状态也改为停售
                    LambdaUpdateWrapper<Dish> updateWrapper = new LambdaUpdateWrapper();
                    updateWrapper.eq(Dish::getId,id);
                    updateWrapper.set(Dish::getStatus,status);
                    this.update(updateWrapper);
                }
            }
        }
    }
}
